/*
 description:	Card drawing subs
 */

#include "../includes/CardGraphics.h"
#include <stdlib.h>
#include <time.h>

const unsigned int NUMBER_OF_CARDS = 10;

int const CARDBACKLOC = (NUMBER_OF_CARDS+2) * CARD_WIDTH; //modify to allow custom card backs

SDL_Surface *deck;
SDL_Surface *scr;

SDL_Rect SrcBack;
SDL_Rect SrcCard;

int InitDeck(SDL_Surface *s) // modify to allow initialization from .ini file
{ // for custom card graphics, custom window background, custom card back
	deck = SDL_LoadBMP("gfx/work-plus.bmp");
	scr = s;

	if (deck == NULL) // if custom card graphics and file not found, revert to default card graphics
	{
		printf("Unable to load deck.bmp: %s\n", SDL_GetError());
		return -1;
	}

	SrcBack.x = CARDBACKLOC;
	SrcBack.y = 0;
	SrcBack.w = CARD_WIDTH;
	SrcBack.h = CARD_HEIGHT;

	SrcCard.y = 0;
	SrcCard.w = CARD_WIDTH;
	SrcCard.h = CARD_HEIGHT;

	// might as well enable setting own transparent color
	SDL_SetColorKey(deck, SDL_SRCCOLORKEY, SDL_MapRGB(deck->format, 0, 255, 0));
	return 0;
}

int DrawCard(int x, int y, int nCdIndex) {
	DrawCard(x, y, nCdIndex, NULL);
}

int DrawCard(int x, int y, int nCdIndex, SDL_Surface *s) {
	if (s == NULL)
		s = scr;

	if (nCdIndex < 0)
		nCdIndex = 0;
	if (nCdIndex > (NUMBER_OF_CARDS-1))
		nCdIndex = (NUMBER_OF_CARDS-1);

	SrcCard.x = nCdIndex * CARD_WIDTH;

	SDL_Rect dest;
	dest.x = x;
	dest.y = y;

	return SDL_BlitSurface(deck, &SrcCard, s, &dest);
}

int DrawCardBack(int x, int y) {
	DrawCardBack(x, y, NULL);
}

int DrawCardBack(int x, int y, SDL_Surface *s) {
	if (s == NULL)
		s = scr;

	SDL_Rect dest;
	dest.x = x;
	dest.y = y;

	return SDL_BlitSurface(deck, &SrcBack, s, &dest);
}

/*
 Symbols:
 0:	Blank, no symbol
 1:	X symbol
 2:	O symbol
 3:	Holder symbol
 */
/*int DrawSymbol(int x, int y, int nSymbol) {
	DrawSymbol(x, y, nSymbol, NULL);
}

int DrawSymbol(int x, int y, int nSymbol, SDL_Surface *s) {
	if (nSymbol < 1)
		return 0;
	if (nSymbol > 3)
		nSymbol = 3;

	if (s == NULL)
		s = scr;

	SrcCard.x = (nSymbol + NUMBER_OF_CARDS) * CARDWIDTH;

	SDL_Rect dest;
	dest.x = x;
	dest.y = y;

	return SDL_BlitSurface(deck, &SrcCard, s, &dest);
}*/

/*
 x,y = position of card
 xspeed = initial horizontal speed
 yspeed = 0
 gravity = constant of gravity, tweak this to get the effect you want
 for every frame
 yspeed = yspeed + gravity
 x = x + xspeed
 y = y + yspeed
 if y > MAXY:
 y = MAXY
 yspeed = -yspeed * 0.8          # tweak this constant to change how high the card bounces
 draw card at (x,y)
 */

// Thanks to Clemens for the pseudocode
/*
 int AnimateCard(CCard card)
 {
 return AnimateCard(card, NULL);
 }
 int AnimateCard(CCard card, SDL_Surface *s)
 */
int AnimateCards() {
	srand((unsigned) time(NULL));

	int rot;
	int id;
	int x;
	int y;
	int xspeed;
	int yspeed;

	int GRAVITY = 1;
	int MAXY = scr->h;
	float BOUNCE = 0.8;

	do {
		rot = rand() % 2;
		id = rand() % (NUMBER_OF_CARDS-1);
		x = rand() % scr->w;
		y = rand() % scr->h / 2;
		/*
		 if(x + (CARDWIDTH / 2) > (scr->w / 2)) //animate to the left
		 xspeed = -4; //pixels to move
		 else
		 xspeed =  4;
		 */
		if (rot)
			xspeed = -4;
		else
			xspeed = 4;

		yspeed = 0;

		do //while card is within the screen
		{
			SDL_PumpEvents();
			if (SDL_GetMouseState(NULL, NULL))
				return -1; // stop the animation

			yspeed = yspeed + GRAVITY;
			x += xspeed;
			y += yspeed;

			if (y + CARD_HEIGHT > MAXY) {
				y = MAXY - CARD_HEIGHT;
				yspeed = int(-yspeed * BOUNCE);
			}

			DrawCard(x, y, id, scr);
			SDL_Flip(scr);
		}
		//73 here is CARDWIDTH, but when using CARDWIDTH, it doesn't work
		while ((x + 123 > 0) && (x < scr->w));
		//		while((x + CARDWIDTH > 0) && (x < scr->w));
	} while (1); //or while within specified time

	return 0;
}

